package com.ims.framework.web.service;

import com.ims.common.core.domain.entity.SysDept;
import com.ims.common.core.domain.entity.SysUser;
import com.ims.common.core.domain.model.LoginUser;
import com.ims.common.enums.UserStatus;
import com.ims.common.exception.ServiceException;
import com.ims.common.utils.StringUtils;
import com.ims.system.domain.TSysSite;
import com.ims.system.service.ISysUserService;
import com.ims.system.service.ITSysSiteService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * 用户验证处理
 *
 * @author IMS
 */
@Service
public class UserDetailsServiceImpl implements UserDetailsService
{
    private static final Logger log = LoggerFactory.getLogger(UserDetailsServiceImpl.class);

    @Autowired
    private ISysUserService userService;

    @Autowired
    private ITSysSiteService sysSiteService;

    @Autowired
    private SysPasswordService passwordService;

    @Autowired
    private SysPermissionService permissionService;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException
    {
        SysUser user = userService.selectUserByUserName(username);
        if (StringUtils.isNull(user))
        {
            log.info("登录用户：{} 不存在.", username);
            throw new ServiceException("登录用户：" + username + " 不存在");
        }
        else if (UserStatus.DELETED.getCode().equals(user.getDelFlag()))
        {
            log.info("登录用户：{} 已被删除.", username);
            throw new ServiceException("对不起，您的账号：" + username + " 已被删除");
        }
        else if (UserStatus.DISABLE.getCode().equals(user.getStatus()))
        {
            log.info("登录用户：{} 已被停用.", username);
            throw new ServiceException("对不起，您的账号：" + username + " 已停用");
        }

        passwordService.validate(user);

        // 公司编码获取
        SysDept dept = user.getDept();
        TSysSite site = sysSiteService.selectTSysSiteByCode(dept.getSiteCode());
        String custId = "";
        String siteCode = "";
        String siteName = "";
        String taxNo = "";
        if(Objects.isNull(site)){
            // 税号为空，则返回默认企业
            site = sysSiteService.selectDefaultSysSite();
        }
        custId = site.getCustId();
        siteCode = site.getSiteCode();
        siteName = site.getSiteName();
        taxNo = site.getTaxNumber();

        return createLoginUser(user, custId, siteCode, siteName, taxNo);
    }

    public UserDetails createLoginUser(SysUser user, String custId, String siteCode, String siteName, String taxNo)
    {
        return new LoginUser(user.getUserId(), custId, siteCode, siteName, taxNo, user.getDeptId(), user, permissionService.getMenuPermission(user));
    }
}
